#!/usr/local/bin/ruby -w
#exp.rb
#This is the main script which is resposible for the execution of the packet-monitoring system for orbit
# This script configure 3 sniffers nodes and put them into monitor mode
# It then call tcpdump and dumps the data into the database and after the experiment is over analyze code is called
# to do analysis for spillover event
# This script is used for testing the system
# Author::    Akshay jog (mailto:akshayj@eden.rutgers.edu)
# Project : Packet Monitoring System for Orbit
# License::   Distributes under the same terms as Ruby
#

Experiment.name = "tutorial-1a"
Experiment.project = "orbit:tutorial"
#
# Define nodes used in experiment
#
#Define node 1,2 as sender with 1024 byte CBR traffic at 300 kbps using UDP

defGroup('sender1', [1,3]) {|node|
  node.image = nil  # assume the right image to be on disk

  node.prototype("test:proto:sender", {
    'destinationHost' => '192.168.1.5',
    'packetSize' => 1024,   #Packet size
    'rate' => 300,          #Offered load = 300 kbps
    'protocol' => 'udp'     #Protocol = UDP
  })
  node.net.w0.mode = "master" #802.11 Master Mode
  node.net.w0.channel="1"
node.net.w0.ip="192.168.1.3"
node.net.w0.essid="pair1"
  
}

defGroup('receiver1', [1,5]) {|node|
  node.image = nil  # assume the right image to be on disk
  node.prototype("test:proto:receiver" , {
    'protocol' => 'udp'   #Use UDP to receive
  })
  node.net.w0.mode = "managed"
  node.net.w0.channel="1"
node.net.w0.ip="192.168.1.5"
node.net.w0.essid="pair1"

}


defGroup('sender2', [2,3]) {|node|
  node.image = nil  # assume the right image to be on disk

  node.prototype("test:proto:sender", {
    'destinationHost' => '192.168.2.2',
    'packetSize' => 1024,   #Packet size
    'rate' => 300,          #Offered load = 300 kbps
    'protocol' => 'udp'     #Protocol = UDP
  })
  node.net.w0.mode = "master" #802.11 Master Mode
  node.net.w0.channel="4"
  node.net.w0.ip="192.168.2.3"
node.net.w0.essid="pair2"
}

defGroup('receiver2', [2,2]) {|node|
  node.image = nil  # assume the right image to be on disk
  node.prototype("test:proto:receiver" , {
    'protocol' => 'udp'   #Use UDP to receive
  })
  node.net.w0.mode = "managed"
  node.net.w0.channel="4"
  node.net.w0.ip="192.168.2.2"
node.net.w0.essid="pair2"

}


defGroup('sender3', [3,3]) {|node|
  node.image = nil  # assume the right image to be on disk

  node.prototype("test:proto:sender", {
    'destinationHost' => '192.168.3.2',
    'packetSize' => 1024,   #Packet size
    'rate' => 300,          #Offered load = 300 kbps
    'protocol' => 'udp'     #Protocol = UDP
  })
  node.net.w0.mode = "master" #802.11 Master Mode
  node.net.w0.channel="7"
  node.net.w0.ip="192.168.3.3"
node.net.w0.essid="pair3"
}

defGroup('receiver3', [3,2]) {|node|
  node.image = nil  # assume the right image to be on disk
  node.prototype("test:proto:receiver" , {
    'protocol' => 'udp'   #Use UDP to receive
  })
  node.net.w0.mode = "managed"
  node.net.w0.channel="7"
  node.net.w0.ip="192.168.3.2"
node.net.w0.essid="pair3"

}



# defGroup('sender4', [4,3]) {|node|
#   node.image = nil  # assume the right image to be on disk
# 
#   node.prototype("test:proto:sender", {
#     'destinationHost' => '192.168.4.2',
#     'packetSize' => 1024,   #Packet size
#     'rate' => 300,          #Offered load = 300 kbps
#     'protocol' => 'udp'     #Protocol = UDP
#   })
#   node.net.w0.mode = "master" #802.11 Master Mode
#   node.net.w0.channel="2"
#   node.net.w0.ip="192.168.4.3"
# node.net.w0.essid="pair4"
# }
# 
# defGroup('receiver4', [4,2]) {|node|
#   node.image = nil  # assume the right image to be on disk
#   node.prototype("test:proto:receiver" , {
#     'protocol' => 'udp'   #Use UDP to receive
#   })
#   node.net.w0.mode = "managed"
#   node.net.w0.channel="2"
#   node.net.w0.ip="192.168.4.2"
# node.net.w0.essid="pair4"
# 
# }
# 
# 
# defGroup('sender5', [5,3]) {|node|
#   node.image = nil  # assume the right image to be on disk
# 
#   node.prototype("test:proto:sender", {
#     'destinationHost' => '192.168.5.2',
#     'packetSize' => 1024,   #Packet size
#     'rate' => 300,          #Offered load = 300 kbps
#     'protocol' => 'udp'     #Protocol = UDP
#   })
#   node.net.w0.mode = "master" #802.11 Master Mode
#   node.net.w0.channel="5"
#   node.net.w0.ip="192.168.5.3"
# node.net.w0.essid="pair5"
# }
# 
# defGroup('receiver5', [5,2]) {|node|
#   node.image = nil  # assume the right image to be on disk
#   node.prototype("test:proto:receiver" , {
#     'protocol' => 'udp'   #Use UDP to receive
#   })
#   node.net.w0.mode = "managed"
#   node.net.w0.channel="5"
#   node.net.w0.ip="192.168.5.2"
# node.net.w0.essid="pair5"
# 
# }
# 
# 
# defGroup('sender6', [6,3]) {|node|
#   node.image = nil  # assume the right image to be on disk
# 
#   node.prototype("test:proto:sender", {
#     'destinationHost' => '192.168.6.2',
#     'packetSize' => 1024,   #Packet size
#     'rate' => 300,          #Offered load = 300 kbps
#     'protocol' => 'udp'     #Protocol = UDP
#   })
#   node.net.w0.mode = "master" #802.11 Master Mode
#   node.net.w0.channel="11"
#   node.net.w0.ip="192.168.6.3"
# node.net.w0.essid="pair6"
# }
# 
# defGroup('receiver6', [6,2]) {|node|
#   node.image = nil  # assume the right image to be on disk
#   node.prototype("test:proto:receiver" , {
#     'protocol' => 'udp'   #Use UDP to receive
#   })
#   node.net.w0.mode = "managed"
#   node.net.w0.channel="11"
#   node.net.w0.ip="192.168.6.2"
# node.net.w0.essid="pair6"
# 
# }
# 
# 
# defGroup('sender7', [7,3]) {|node|
#   node.image = nil  # assume the right image to be on disk
# 
#   node.prototype("test:proto:sender", {
#     'destinationHost' => '192.168.7.4',
#     'packetSize' => 1024,   #Packet size
#     'rate' => 300,          #Offered load = 300 kbps
#     'protocol' => 'udp'     #Protocol = UDP
#   })
#   node.net.w0.mode = "master" #802.11 Master Mode
#   node.net.w0.channel="8"
#   node.net.w0.ip="192.168.7.3"
# node.net.w0.essid="pair7"
# }
# 
# defGroup('receiver7', [7,4]) {|node|
#   node.image = nil  # assume the right image to be on disk
#   node.prototype("test:proto:receiver" , {
#     'protocol' => 'udp'   #Use UDP to receive
#   })
#   node.net.w0.mode = "managed"
#   node.net.w0.channel="8"
#   node.net.w0.ip="192.168.7.4"
# node.net.w0.essid="pair7"
# 
# }
# 
# 
# defGroup('sender8', [8,3]) {|node|
#   node.image = nil  # assume the right image to be on disk
# 
#   node.prototype("test:proto:sender", {
#     'destinationHost' => '192.168.8.2',
#     'packetSize' => 1024,   #Packet size
#     'rate' => 300,          #Offered load = 300 kbps
#     'protocol' => 'udp'     #Protocol = UDP
#   })
#   node.net.w0.mode = "master" #802.11 Master Mode
#   node.net.w0.channel="3"
#   node.net.w0.ip="192.168.8.3"
# node.net.w0.essid="pair8"
# }
# 
# defGroup('receiver8', [8,2]) {|node|
#   node.image = nil  # assume the right image to be on disk
#   node.prototype("test:proto:receiver" , {
#     'protocol' => 'udp'   #Use UDP to receive
#   })
#   node.net.w0.mode = "managed"
#   node.net.w0.channel="3"
#   node.net.w0.ip="192.168.8.2"
# node.net.w0.essid="pair8"
# 
# }
# 
# 
# defGroup('sender9', [9,3]) {|node|
#   node.image = nil  # assume the right image to be on disk
# 
#   node.prototype("test:proto:sender", {
#     'destinationHost' => '192.168.9.2',
#     'packetSize' => 1024,   #Packet size
#     'rate' => 300,          #Offered load = 300 kbps
#     'protocol' => 'udp'     #Protocol = UDP
#   })
#   node.net.w0.mode = "master" #802.11 Master Mode
#   node.net.w0.channel="6"
#   node.net.w0.ip="192.168.9.3"
# node.net.w0.essid="pair9"
# }
# 
# defGroup('receiver9', [9,2]) {|node|
#   node.image = nil  # assume the right image to be on disk
#   node.prototype("test:proto:receiver" , {
#     'protocol' => 'udp'   #Use UDP to receive
#   })
#   node.net.w0.mode = "managed"
#   node.net.w0.channel="6"
#   node.net.w0.ip="192.168.9.2"
# node.net.w0.essid="pair9"
# 
# }
# 
# 
# defGroup('sender10', [10,3]) {|node|
#   node.image = nil  # assume the right image to be on disk
# 
#   node.prototype("test:proto:sender", {
#     'destinationHost' => '192.168.10.2',
#     'packetSize' => 1024,   #Packet size
#     'rate' => 300,          #Offered load = 300 kbps
#     'protocol' => 'udp'     #Protocol = UDP
#   })
#   node.net.w0.mode = "master" #802.11 Master Mode
#   node.net.w0.channel="10"
#   node.net.w0.ip="192.168.10.3"
# node.net.w0.essid="pair10"
# }
# 
# defGroup('receiver10', [10,2]) {|node|
#   node.image = nil  # assume the right image to be on disk
#   node.prototype("test:proto:receiver" , {
#     'protocol' => 'udp'   #Use UDP to receive
#   })
#   node.net.w0.mode = "managed"
#   node.net.w0.channel="10"
#   node.net.w0.ip="192.168.10.2"
# node.net.w0.essid="pair10"
# 
# }
# 
# 
# 
# defGroup('sender11', [11,3]) {|node|
#   node.image = nil  # assume the right image to be on disk
# 
#   node.prototype("test:proto:sender", {
#     'destinationHost' => '192.168.11.2',
#     'packetSize' => 1024,   #Packet size
#     'rate' => 300,          #Offered load = 300 kbps
#     'protocol' => 'udp'     #Protocol = UDP
#   })
#   node.net.w0.mode = "master" #802.11 Master Mode
#   node.net.w0.channel="9"
#   node.net.w0.ip="192.168.11.3"
# node.net.w0.essid="pair11"
# }
# 
# defGroup('receiver11', [11,2]) {|node|
#   node.image = nil  # assume the right image to be on disk
#   node.prototype("test:proto:receiver" , {
#     'protocol' => 'udp'   #Use UDP to receive
#   })
#   node.net.w0.mode = "managed"
#   node.net.w0.channel="9"
#   node.net.w0.ip="192.168.11.2"
# node.net.w0.essid="pair11"
# 
# }
# 
# 







defGroup('sniffer1',[1,4]){ |node|
node.image=nil
node.prototype("test:proto:receiver" ,  {
'protocol'=> 'udp'
})

#node.net.w0.mode="monitor"
node.net.w0.channel="1"
node.net.w0.ip="192.168.1.4"
node.net.w0.essid="pair1"
}


defGroup('sniffer2',[2,4]){ |node|
node.image=nil
node.prototype("test:proto:receiver" ,  {
'protocol'=> 'udp'
})

#node.net.w0.mode="monitor"
node.net.w0.channel="4"
node.net.w0.ip="192.168.2.4"
node.net.w0.essid="pair2"
}
defGroup('sniffer3',[3,4]){ |node|
node.image=nil
node.prototype("test:proto:receiver" ,  {
'protocol'=> 'udp'
})

#node.net.w0.mode="monitor"
node.net.w0.channel="7"
node.net.w0.ip="192.168.3.4"
node.net.w0.essid="pair3"
}
# defGroup('sniffer4',[4,4]){ |node|
# node.image=nil
# node.prototype("test:proto:receiver" ,  {
# 'protocol'=> 'udp'
# })
# 
# #node.net.w0.mode="monitor"
# node.net.w0.channel="2"
# node.net.w0.ip="192.168.4.4"
# node.net.w0.essid="pair4"
# }
# defGroup('sniffer5',[5,4]){ |node|
# node.image=nil
# node.prototype("test:proto:receiver" ,  {
# 'protocol'=> 'udp'
# })
# 
# #node.net.w0.mode="monitor"
# node.net.w0.channel="5"
# node.net.w0.ip="192.168.5.4"
# node.net.w0.essid="pair5"
# }
# defGroup('sniffer6',[6,4]){ |node|
# node.image=nil
# node.prototype("test:proto:receiver" ,  {
# 'protocol'=> 'udp'
# })
# 
# #node.net.w0.mode="monitor"
# node.net.w0.channel="11"
# node.net.w0.ip="192.168.6.4"
# node.net.w0.essid="pair6"
# }
# defGroup('sniffer7',[7,5]){ |node|
# node.image=nil
# node.prototype("test:proto:receiver" ,  {
# 'protocol'=> 'udp'
# })
# 
# #node.net.w0.mode="monitor"
# node.net.w0.channel="8"
# node.net.w0.ip="192.168.7.5"
# node.net.w0.essid="pair7"
# }
# defGroup('sniffer8',[8,4]){ |node|
# node.image=nil
# node.prototype("test:proto:receiver" ,  {
# 'protocol'=> 'udp'
# })
# 
# #node.net.w0.mode="monitor"
# node.net.w0.channel="3"
# node.net.w0.ip="192.168.8.4"
# node.net.w0.essid="pair8"
# }
# defGroup('sniffer9',[9,4]){ |node|
# node.image=nil
# node.prototype("test:proto:receiver" ,  {
# 'protocol'=> 'udp'
# })
# 
# #node.net.w0.mode="monitor"
# node.net.w0.channel="6"
# node.net.w0.ip="192.168.9.4"
# node.net.w0.essid="pair9"
# }
# defGroup('sniffer10',[10,4]){ |node|
# node.image=nil
# node.prototype("test:proto:receiver" ,  {
# 'protocol'=> 'udp'
# })
# 
# #node.net.w0.mode="monitor"
# node.net.w0.channel="10"
# node.net.w0.ip="192.168.10.4"
# node.net.w0.essid="pair10"
# }
# defGroup('sniffer11',[11,4]){ |node|
# node.image=nil
# node.prototype("test:proto:receiver" ,  {
# 'protocol'=> 'udp'
# })
# 
# #node.net.w0.mode="monitor"
# node.net.w0.channel="9"
# node.net.w0.ip="192.168.11.4"
# node.net.w0.essid="pair11"
# }



allGroups.net.w0 { |w|
  w.type = 'b'              #Set all nodes to use 802.11b
 #w.essid = "helloworld"    #essid = helloworld
 w.ip = "%192.168.%x.%y"   #IP address = 192.168.x.y
 #w.channel="6"
}


allGroups.exec("modprobe ath_pci")

group('sniffer1').exec("modprobe ath_pci")
group('sniffer1').exec("ifconfig ath0 down")
group('sniffer1').net.w0{
|w|
w.mode="monitor"
w.channel="1"
}

group('sniffer2').exec("ifconfig ath0 down")
group('sniffer2').net.w0{
|w|
w.mode="monitor"
w.channel="4"
}

group('sniffer3').exec("ifconfig ath0 down")
group('sniffer3').net.w0{
|w|
w.mode="monitor"
w.channel="7"
}
# group('sniffer4').exec("ifconfig ath0 down")
# group('sniffer4').net.w0{
# |w|
# w.mode="monitor"
# w.channel="2"
# }
# group('sniffer5').exec("ifconfig ath0 down")
# group('sniffer5').net.w0{
# |w|
# w.mode="monitor"
# w.channel="5"
# }
# group('sniffer6').exec("ifconfig ath0 down")
# group('sniffer6').net.w0{
# |w|
# w.mode="monitor"
# w.channel="11"
# }
# group('sniffer7').exec("ifconfig ath0 down")
# group('sniffer7').net.w0{
# |w|
# w.mode="monitor"
# w.channel="8"
# }
# group('sniffer8').exec("ifconfig ath0 down")
# group('sniffer8').net.w0{
# |w|
# w.mode="monitor"
# w.channel="3"
# }
# group('sniffer9').exec("ifconfig ath0 down")
# group('sniffer9').net.w0{
# |w|
# w.mode="monitor"
# w.channel="6"
# }
# group('sniffer10').exec("ifconfig ath0 down")
# group('sniffer10').net.w0{
# |w|
# w.mode="monitor"
# w.channel="10"
# }
# group('sniffer11').exec("ifconfig ath0 down")
# group('sniffer11').net.w0{
# |w|
# w.mode="monitor"
# w.channel="9"
# }
# 
# 
# 
# wait 1
allGroups.exec("ifconfig ath0 up")


#wait 2





#
# Now, start the application
#
whenAllInstalled() {|node|
  wait 10
  allGroups.startApplications
wait 2


#allGroups.exec("tcpdump -i ath0 -s 1500 -n -q -e >/home/dump_naya.txt")

allGroups.exec("sh /home/mysql-collect.sh > /home/collect-ath0.txt")


#group('sniffer1').exec("tcpdump -i ath0 -s 1500 -n -q -e >/home/dump_naya.txt")
#group('sniffer1').exec("sh /home/mysql-collect.sh")

#allGroups.exec("sh /home/mysql-collect.sh")
 wait 2

#group('sniffer1').exec("pkill tcpdump")

allGroups.exec("pkill tcpdump")
 
# #pkill


group('sniffer1').exec("ruby /home/database.rb")
group('sniffer2').exec("ruby /home/database.rb")
group('sniffer3').exec("ruby /home/database.rb")
# group('sniffer4').exec("ruby /home/database.rb")
# group('sniffer5').exec("ruby /home/database.rb")
# group('sniffer6').exec("ruby /home/database.rb")
# group('sniffer7').exec("ruby /home/database.rb")
# group('sniffer8').exec("ruby /home/database.rb")
# group('sniffer9').exec("ruby /home/database.rb")
# group('sniffer10').exec("ruby /home/database.rb")
# group('sniffer11').exec("ruby /home/database.rb")

wait 10

#group('sniffer3').exec("ruby /home/analyze.rb")




wait 20

Experiment.done

}
